/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.hohai.river.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hohai.river.entity.River;
import com.hohai.river.service.RiverService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author pig code generator
 * @date 2022-09-11 17:18:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/river")
@Tag(name = "管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiverController {

	private final RiverService riverService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param river
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('river_river_get')")
	public R getRiverPage(Page page, River river) {
		PigUser user = SecurityUtils.getUser();

		LambdaQueryWrapper<River> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StringUtils.isNotEmpty(river.getName()), River::getName, river.getName())
				.eq(river.getLevel() != null, River::getLevel, river.getLevel())
				.eq(StringUtils.isNotEmpty(river.getYear()), River::getYear, river.getYear());

		if (user.getCityId() > 0) {
			wrapper.in(River::getCityId, user.getCityId());
			return R.ok(riverService.page(page, wrapper));
		}
		else {
			return R.ok(riverService.page(page, wrapper));
		}
	}

	/**
	 * 通过id查询
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('river_river_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(riverService.getById(id));
	}

	/**
	 * 新增
	 * @param river
	 * @return R
	 */
	@Operation(summary = "新增", description = "新增")
	@SysLog("新增")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('river_river_add')")
	public R save(@RequestBody River river) {
		return R.ok(riverService.save(river));
	}

	/**
	 * 修改
	 * @param river
	 * @return R
	 */
	@Operation(summary = "修改", description = "修改")
	@SysLog("修改")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('river_river_edit')")
	public R updateById(@RequestBody River river) {
		return R.ok(riverService.updateById(river));
	}

	/**
	 * 通过id删除
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除", description = "通过id删除")
	@SysLog("通过id删除")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('river_river_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(riverService.removeById(id));
	}

	/**
	 * 更新系统总分
	 */
	@Operation(summary = "更新系统总分", description = "更新系统总分")
	@SysLog("更新系统总分")
	@PutMapping("{riverId}")
	@PreAuthorize("@pms.hasPermission('river_river_edit')")
	public R updateSystemScore(@PathVariable("riverId") Long riverId, @RequestParam("score") Double score) {
		return R.ok(riverService.updateSystemScore(riverId, score));
	}

	/**
	 * 查询所有指标分数
	 */
	@Operation(summary = "所有指标分数", description = "所有指标分数")
	@GetMapping("/viewScores")
	@PreAuthorize("@pms.hasPermission('river_report_audit')")
	public R getExpertFillList(@RequestParam("riverId") Long riverId) {
		return R.ok(riverService.getAllIndexScores(riverId));
	}

}
